package org.springblade.modules.bank.util;

import cn.hutool.core.util.NumberUtil;
import org.springblade.modules.bank.entity.TmExcelImportLog;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ThreadLocalRandom;

public class BusFileUtil {
    public static void uploadFile(TmExcelImportLog tmExcelImportLog,MultipartFile file,String recordFilePath) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String today = LocalDate.now().format(dtf);
        File pathTemp = new File(recordFilePath + "/" + "excel/" + today  + "/");
        if(!pathTemp.exists()){
            pathTemp.mkdirs();
        }

        String fileName = ThreadLocalRandom.current().nextInt() +"_"+file.getOriginalFilename();
        String filePath = "excel/" + today + "/" +fileName;

        tmExcelImportLog.setFileName(fileName);
        tmExcelImportLog.setFileSize(file.getSize());
        tmExcelImportLog.setFileType(file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")));
        tmExcelImportLog.setFilePath(filePath);

        BufferedOutputStream out = null;
        try {
            out = new BufferedOutputStream(
                    new FileOutputStream(new File(recordFilePath+"/"+filePath)));
            out.write(file.getBytes());
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
